using System.Windows.Forms;
using Greg.XmlEditor.Presentation.Views;

namespace Greg.XmlEditor.Presentation
{
	public class InteractionManager : IInteractionManager
	{
		public DialogResult AskIfSaveDocument()
		{
			return MessageBox.Show("The document has been modified, would you save it?",
			                       "Save document", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
		}

		public void ShowError(string errorMessage)
		{
			MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
		}

		public bool AskWhereToSaveFile(ref string fileName)
		{
			using (var fd = new SaveFileDialog())
			{
				fd.Title = "Save as...";
				fd.FileName = fileName;
				fd.Filter = "XML File (*.xml)|*.xml|XSD Schema (*.xsd)|*.xsd|All files(*.*)|*.*";

				if (fd.ShowDialog() != DialogResult.OK) return false;

				fileName = fd.FileName;
				return true;
			}
		}

		public bool AskFromWhereOpenFile(ref string fileName)
		{
			using (var fd = new OpenFileDialog())
			{
				fd.Title = "Open...";
				fd.FileName = fileName;
				fd.Filter = "XML File (*.xml)|*.xml|XSD Schema (*.xsd)|*.xsd|All files(*.*)|*.*";

				if (fd.ShowDialog() != DialogResult.OK) return false;

				fileName = fd.FileName;
				return true;
			}
		}

		public bool AskForSchemaFile(ref string fileName)
		{
			using (var fd = new OpenFileDialog())
			{
				fd.Title = "Select an XSD schema";
				fd.FileName = fileName;
				fd.Filter = "XSD Schema (*.xsd)|*.xsd";

				if (fd.ShowDialog() != DialogResult.OK) return false;

				fileName = fd.FileName;
				return true;
			}
		}

		public bool AskForStyleFile(ref string fileName)
		{
			using (var fd = new OpenFileDialog())
			{
				fd.Title = "Select an XSLT file";
				fd.FileName = fileName;
				fd.Filter = "XSLT File (*.xslt,*.xsl)|*.xslt;*.xsl";

				if (fd.ShowDialog() != DialogResult.OK) return false;

				fileName = fd.FileName;
				return true;
			}
		}

		public bool AskIfSaveFilesOnClose()
		{
			return MessageBox.Show("There are documents that have been modified, would you save them?", "Save documents...",
			                       MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
		}

		public bool AskIfCancelClosing()
		{
			return MessageBox.Show("There are documents that have been modified and not been saved yet. Would you save those files before closing?", "Save documents...",
			                       MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
		}

		public void ShowTrasformedDocument(string trasformedFilePath)
		{
			using (var dialog = new BrowserWindow(trasformedFilePath))
			{
				dialog.ShowDialog();
			}
		}
	}
}